﻿//Copyright (c) 2011 Shane Baker
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

namespace XRGLib
{
    /// <summary>
    /// A wrapper around System.Random providing some helper methods for getting random values.
    /// </summary>
    public class Rand
    {
        Random rnd;

        /// <summary>
        /// Construct a default Rand instance
        /// </summary>
        public Rand()
        {
            rnd = new Random();
        }

        /// <summary>
        /// Construct a Rand instance with the provided seed value
        /// </summary>
        /// <param name="seed"></param>
        public Rand(int seed)
        {
            rnd = new Random(seed);
        }

        /// <summary>
        /// Construct a Rand instance from the provided System.Random instance
        /// </summary>
        /// <param name="random"></param>
        public Rand(Random random)
        {
            rnd = random;
        }

        /// <summary>
        /// Returns 0 or 1
        /// </summary>
        /// <returns></returns>
        public int GetInt()
        {
            return rnd.Next(0, 2);
        }

        /// <summary>
        /// Returns a random value from minimum to maximum, inclusive
        /// </summary>
        /// <param name="minimum"></param>
        /// <param name="maximum"></param>
        /// <returns></returns>
        public int GetInt(int minimum, int maximum)
        {
            return rnd.Next(minimum, maximum + 1);
        }

        /// <summary>
        /// Returns a randomly generated integer based on the specified Dice object.
        /// </summary>
        /// <param name="dice"></param>
        /// <returns></returns>
        public int GetInt(Dice dice)
        {
            if (dice.NumberOfDice < 1 || dice.NumberOfSides < 1)
            {
                return dice.Modifier;
            }

            int ret = 0;

            for (int i = 0; i < dice.NumberOfDice; i++)
            {
                ret += rnd.Next(1, dice.NumberOfSides + 1);
            }

            return ret + dice.Modifier;
        }

        /// <summary>
        /// Has an equal chance of returning true or false.
        /// </summary>
        /// <returns></returns>
        public bool GetBoolean()
        {
            return (GetInt() == 0);
        }

        /// <summary>
        /// Random float between 0.0 and 1.0
        /// </summary>
        /// <returns></returns>
        public float GetFloat()
        {
            return (float)rnd.NextDouble();
        }

        /// <summary>
        /// Randomly chooses an Enum value of the given type.
        /// </summary>
        /// <typeparam name="T">Must be an enum, or an exception will be thrown.</typeparam>
        /// <returns></returns>
        public T GetEnum<T>()
        {
            T[] values = (T[])Enum.GetValues(typeof(T));

            return values[rnd.Next(0, values.Length)];
        }

        /// <summary>
        /// Return a Point with randomized x and y values.
        /// </summary>
        /// <param name="xmin"></param>
        /// <param name="ymin"></param>
        /// <param name="xmax"></param>
        /// <param name="ymax"></param>
        /// <returns></returns>
        public Point GetPoint(int xmin, int ymin, int xmax, int ymax)
        {
            int x = GetInt(xmin, xmax);
            int y = GetInt(ymin, ymax);

            return new Point(x, y);
        }

        /// <summary>
        /// Returns a random Color
        /// </summary>
        /// <returns></returns>
        public Color GetColor()
        {
            return new Color(GetInt(0, 255), GetInt(0, 255), GetInt(0, 255));
        }

        /// <summary>
        /// Interpolates between the two specified colors by a random amount
        /// </summary>
        /// <param name="col1"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public Color GetColorBetween(Color col1, Color col2)
        {
            float amount = GetFloat();
            return Color.Lerp(col1, col2, amount);
        }

        /// <summary>
        /// Randomly chooses a single character from the provided string.
        /// </summary>
        /// <param name="str">Must not be null or empty, or this method will throw an ArgumentException</param>
        /// <returns></returns>
        public char GetRandomCharacter(string str)
        {
            if (string.IsNullOrEmpty(str))
                throw new ArgumentException("The string cannot be null or empty", "str");

            int pos = GetInt(0, str.Length - 1);
            return str[pos];
        }
    }
}
